<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="乘客id" prop="passengersId">
        <el-input
          v-model="queryParams.passengersId"
          placeholder="请输入乘客id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机id" prop="driversId">
        <el-input
          v-model="queryParams.driversId"
          placeholder="请输入司机id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上车地点" prop="pickupLocation">
        <el-input
          v-model="queryParams.pickupLocation"
          placeholder="请输入上车地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-input
          v-model="queryParams.destination"
          placeholder="请输入目的地"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预估价格" prop="estimatedPrice">
        <el-input
          v-model="queryParams.estimatedPrice"
          placeholder="请输入预估价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际价格" prop="actualPrice">
        <el-input
          v-model="queryParams.actualPrice"
          placeholder="请输入实际价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠卷" prop="couponId">
        <el-input
          v-model="queryParams.couponId"
          placeholder="请输入优惠卷"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型id" prop="vehicleId">
        <el-input
          v-model="queryParams.vehicleId"
          placeholder="请输入车型id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户订单表 id不可自增 统一雪花算法生成" align="center" prop="id" />
      <el-table-column label="乘客id" align="center" prop="passengersId" />
      <el-table-column label="司机id" align="center" prop="driversId" />
      <el-table-column label="上车地点" align="center" prop="pickupLocation" />
      <el-table-column label="目的地" align="center" prop="destination" />
      <el-table-column label="订单状态0：待接单 1：已接单 2：行程中3：已完成未支付4.已支付" align="center" prop="orderStatus" />
      <el-table-column label="预估价格" align="center" prop="estimatedPrice" />
      <el-table-column label="实际价格" align="center" prop="actualPrice" />
      <el-table-column label="优惠卷" align="center" prop="couponId" />
      <el-table-column label="车型id" align="center" prop="vehicleId" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="订单号" align="center" prop="outTradeNo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:order:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"

            @click="showWeChatPayQRCode(scope.row.outTradeNo,scope.row.estimatedPrice)">微信支付</el-button>
          <el-button @click="alibabaPay(scope.row.outTradeNo,scope.row.estimatedPrice)"
                  size="mini"
                  type="text"
                  >支付宝支付</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改order对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="乘客id" prop="passengersId">
          <el-input v-model="form.passengersId" placeholder="请输入乘客id" />
        </el-form-item>
        <el-form-item label="司机id" prop="driversId">
          <el-input v-model="form.driversId" placeholder="请输入司机id" />
        </el-form-item>
        <el-form-item label="上车地点" prop="pickupLocation">
          <el-input v-model="form.pickupLocation" placeholder="请输入上车地点" />
        </el-form-item>
        <el-form-item label="目的地" prop="destination">
          <el-input v-model="form.destination" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="预估价格" prop="estimatedPrice">
          <el-input v-model="form.estimatedPrice" placeholder="请输入预估价格" />
        </el-form-item>
        <el-form-item label="实际价格" prop="actualPrice">
          <el-input v-model="form.actualPrice" placeholder="请输入实际价格" />
        </el-form-item>
        <el-form-item label="优惠卷" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠卷" />
        </el-form-item>
        <el-form-item label="车型id" prop="vehicleId">
          <el-input v-model="form.vehicleId" placeholder="请输入车型id" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <img :src="weChatPayQRCodeUrl" alt="微信支付二维码" />
<!--    <div v-if="showQRCodeDialog" class="dialog">-->
<!--      <button @click="hideWeChatPayQRCode">关闭</button>-->
<!--    </div>-->
  </div>
</template>

<script>
import order, {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  openWechatImage,
  myAlipay,
  payType
} from "@/api/user/order";
import {html} from "js-beautify/js/src";
import axios from "axios";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // order表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 微信支付二维码弹窗
      showQRCodeDialog: false,
      weChatPayQRCodeUrl: "", // 请替换为实际的二维码图片 URL,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        passengersId: null,
        driversId: null,
        pickupLocation: null,
        destination: null,
        orderStatus: null,
        estimatedPrice: null,
        actualPrice: null,
        couponId: null,
        vehicleId: null,
        longitude: null,
        latitude: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    showWeChatPayQRCode(outTradeNo,estimatedPrice) {
      // openWechatImage(outTradeNo,estimatedPrice).then(
      //   res=>{
      //     this.showQRCodeDialog = true;
      //     this.weChatPayQRCodeUrl = res.data.qrcode;
      //   }
      // )
      payType('wechat',outTradeNo,estimatedPrice).then(
        res=>{
          console.log(res.data)
          this.showQRCodeDialog = true;
          this.weChatPayQRCodeUrl = res.data.qrcode;
        }
      )
    },
    alibabaPay(outTradeNo,estimatedPrice){
      //http://localhost:9205/order/pay/alipay/178974548948/56
      // window.open("http://localhost:9205/order/aliPay/"+outTradeNo+"/"+estimatedPrice);
      payType('alipay',outTradeNo,estimatedPrice).then(
        res=>{
          console.log(res)
          window.open(res.data);
        }
      )
      // aliPay(outTradeNo,estimatedPrice).then(
      //   res=>{
      //     console.log(res);
      //     // window.open(res.data);
      //   }
      // )
    },
    hideWeChatPayQRCode() {
      this.showQRCodeDialog = false;
    },
    /** 查询order列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        passengersId: null,
        driversId: null,
        pickupLocation: null,
        destination: null,
        orderStatus: null,
        estimatedPrice: null,
        actualPrice: null,
        createTime: null,
        updateTime: null,
        couponId: null,
        vehicleId: null,
        longitude: null,
        latitude: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加order";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改order";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除order编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
